package com.beemans.admob.demo.ui.fragments

import android.util.Log
import com.beemans.common.ui.fragments.CommonFragment
import com.beemans.common.utils.ToastUtils
import com.beemans.admob.ads.AbAdvertManager
import com.beemans.admob.ads.AbNativeAdvert
import com.beemans.admob.ads.AbNativeAdvertCallback
import com.beemans.admob.ads.AbNativeAdvertConfig
import com.beemans.admob.demo.R
import com.beemans.admob.demo.constant.Constant
import com.beemans.admob.demo.databinding.FragmentNativeAdBinding
import com.beemans.admob.demo.ui.view.CustomAbNativeRender
import com.google.android.gms.ads.ResponseInfo
import com.tiamosu.databinding.delegate.lazyDataBindingOrNull
import com.tiamosu.databinding.page.DataBindingConfig
import com.tiamosu.fly.ext.clickNoRepeat

/**
 * @author tiamosu
 * @date 2022/10/31
 */
class NativeAdFragment : CommonFragment() {
    private val dataBinding: FragmentNativeAdBinding? by lazyDataBindingOrNull { }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.fragment_native_ad)
    }

    override fun initEvent() {
        //预加载广告
        dataBinding?.nativeAdBtnPreload?.clickNoRepeat {
            loadAdvert()
        }

        //加载广告并展示
        dataBinding?.nativeAdBtnShow?.clickNoRepeat {
            showAdvert()
        }
    }

    /**
     * 加载广告
     */
    private fun loadAdvert() {
        val advertConfig = AbNativeAdvertConfig(Constant.NATIVE_ID)

        AbAdvertManager.loadNativeAdvert(
            this,
            advertConfig,
            null,
            object : AbNativeAdvertCallback() {
                override fun onAdvertLoaded(abNativeAdvert: AbNativeAdvert) {
                    printLog("广告加载成功")
                }

                override fun onAdvertLoadFail(abNativeAdvert: AbNativeAdvert, adError: String?) {
                    printLog("广告加载失败")
                }
            }
        )
    }

    /**
     * 展示广告
     */
    private fun showAdvert() {
        val advertConfig = AbNativeAdvertConfig(Constant.NATIVE_ID)
        advertConfig.isShowAfterLoaded = true
        advertConfig.isPreloadAdvert = false

        val nativeSelfRender = dataBinding?.nativeAdFlAd?.let {
            CustomAbNativeRender(it, R.layout.layout_native_self2)
        }
        AbAdvertManager.loadNativeAdvert(
            this,
            advertConfig,
            nativeSelfRender,
            object : AbNativeAdvertCallback() {
                override fun onAdvertLoaded(abNativeAdvert: AbNativeAdvert) {
                    printLog("广告加载成功")
                }

                override fun onAdvertLoadFail(abNativeAdvert: AbNativeAdvert, adError: String?) {
                    printLog("广告加载失败")
                }

                override fun onAdvertShow(
                    abNativeAdvert: AbNativeAdvert,
                    responseInfo: ResponseInfo?
                ) {
                    printLog("广告展示 --- responseInfo:$responseInfo")
                }

                override fun onAdvertShowFail(abNativeAdvert: AbNativeAdvert) {
                    printLog("广告展示失败")
                }

                override fun onAdvertClicked(
                    abNativeAdvert: AbNativeAdvert,
                    responseInfo: ResponseInfo?
                ) {
                    printLog("广告点击 --- responseInfo:$responseInfo")
                }

                override fun onAdvertClose(
                    abNativeAdvert: AbNativeAdvert,
                    responseInfo: ResponseInfo?
                ) {
                    printLog("广告关闭 --- responseInfo:$responseInfo")
                }
            }
        )
    }

    private fun printLog(msg: String) {
        Log.e("susu", msg)
        ToastUtils.showShort(msg)
    }

    override fun doBusiness() {
    }
}